//
//  TSAccountAddVipViewController.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/23.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSAccountAddVipViewController: TSBaseViewController {
    
    var tableView : UITableView!
    var accountField : UITextField!
    var passwordField : UITextField!
    var confirmField : UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationTitle(title: "增加会员")
        configViews()
    }
    
    @objc func registerAction() {
        
        if checkCommitCondition() {
            CBToast.showToastAction()
            TSNetworkManager.request(TMSNetworkTools.register(username: accountField.text!, password: passwordField.text!, repassword: confirmField.text!), showHUD: true, success: { (data) in
                CBToast.hiddenToastAction()
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
                    CBToast.showToastAction(message: "注册成功")
                }
                self.dismiss(animated: true, completion: nil)
            }) { (error) in
                CBToast.hiddenToastAction()
            }
        }
    }
    
    func checkCommitCondition() -> Bool {
        
        guard accountField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入账号")
            return false
        }
        
        guard passwordField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入登录密码")
            return false
        }
        
        guard confirmField.text?.count != 0 else {
            CBToast.showToastAction(message: "请确认登录密码")
            return false
        }
        
        guard passwordField.text == confirmField.text else {
            CBToast.showToastAction(message: "新密码与确认密码输入不一致")
            return false
        }
        
        return true
    }
    
}

extension TSAccountAddVipViewController {
    
    func configViews() {

        tableView = UITableView.init(frame: CGRect(x: 0, y: CGFloat(T_NAVBARHEIGHT), width: SCREEN_WIDTH, height: SCREEN_HEIGHT - CGFloat(T_NAVBARHEIGHT)), style: .plain)
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(tableView)
        
        accountField = createInputField(placeholder: "请输入账号")
        tableView.addSubview(accountField)
        accountField.snp.makeConstraints { (make) in
            make.left.equalTo(26)
            make.top.equalTo(31)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*26, height: 50))
        }
        
        let line1 = drawLine()
        tableView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalTo(accountField.snp.bottom)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*12, height: 1))
        }
                
        passwordField = createInputField(placeholder: "请输入登录密码")
        passwordField.isSecureTextEntry = true
        tableView.addSubview(passwordField)
        passwordField.snp.makeConstraints { (make) in
            make.left.equalTo(accountField)
            make.top.equalTo(line1.snp.bottom)
            make.size.equalTo(accountField)
        }
        
        let line2 = drawLine()
        tableView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(passwordField.snp.bottom)
            make.size.equalTo(line1)
        }
                        
        confirmField = createInputField(placeholder: "请确认登录密码")
        confirmField.isSecureTextEntry = true
        tableView.addSubview(confirmField)
        confirmField.snp.makeConstraints { (make) in
            make.left.equalTo(accountField)
            make.top.equalTo(line2.snp.bottom)
            make.size.equalTo(accountField)
        }
        
        let line3 = drawLine()
        tableView.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(confirmField.snp.bottom)
            make.size.equalTo(line1)
        }
                                
        
        let registerBtn = UIButton.init(type: .system)
        registerBtn.setTitle("注册", for: .normal)
        registerBtn.setTitleColor(WHITE_COLOR, for: .normal)
        registerBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        registerBtn.backgroundColor = BASE_RED_COLOR
        registerBtn.layer.cornerRadius = 24
        registerBtn.layer.masksToBounds = true
        registerBtn.addTarget(self, action: #selector(registerAction), for: .touchUpInside)
        tableView.addSubview(registerBtn)
        registerBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(line3.snp.bottom).offset(100)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
    }
    
    func createInputField(placeholder:String) -> UITextField {
        
        let attributeStr = NSMutableAttributedString.init(string: placeholder)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: "#989898"), NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: .medium)], range: NSMakeRange(0, placeholder.count))
        
        let textField = UITextField.init()
        textField.attributedPlaceholder = (attributeStr.copy() as! NSAttributedString)
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        return textField
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        return line
    }
}
